#ifndef URLTASK_H
#define URLTASK_H

#include <QObject>
#include <QUrl>
#include <QFile>
#include <QString>
#include <QSharedPointer>

class QNetworkAccessManager;
class QNetworkReply;

class UrlTask : public QObject
{
    Q_OBJECT

private slots:
    void progress_changed(qint64 recv, qint64 total);
    void data_ready();
    void download_finished();

signals:
    void task_changed(UrlTask const &task);

public:
    enum
    {
        STATUS = 0,
        URL = 1,
        PROGRESS = 2,
        FILE = 3,
        LAST = 4
    };

    enum TaskStatus
    {
        INPROGRESS = 0,
        STOPPED = 1,
        FINISHED = 2,
        ERROR = 3,
        STATUS_COUNT = 4
    };

    explicit UrlTask(QString const &url
                     , QString const &file
                     , TaskStatus status
                     , QNetworkAccessManager *manager = 0
                     , QObject *parent = 0);
    virtual ~UrlTask();

    UrlTask(QObject *parent = 0);
    UrlTask(UrlTask const &task);
    UrlTask &operator=(UrlTask const &task);

    QString get_url() const;
    QString get_file() const;
    quint8 get_progress() const;
    TaskStatus get_status() const;

    void start_task();
    void stop_task();

    bool operator==(UrlTask const &task) const;

private:
    QUrl m_url;
    QSharedPointer<QFile> m_file;
    quint8 m_progress;
    TaskStatus m_status;

    QNetworkAccessManager *m_manager;
    QNetworkReply *m_reply;

    void report_error(QString const &err);
    void close();
};

#endif // URLTASK_H
